import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:hm_show_flutter/api/service/goods_service.dart';
import 'package:hm_show_flutter/constant/string.dart';
import 'package:hm_show_flutter/entity/goods.dart';
import 'package:hm_show_flutter/style/style.dart';
import 'package:hm_show_flutter/utils/toast_util.dart';
import 'package:hm_show_flutter/widgets/empty_view.dart';
import 'package:hm_show_flutter/utils/navigator_util.dart';
import 'package:hm_show_flutter/widgets/cached_image.dart';

class SearchGoodsPage extends StatefulWidget {
  @override
  _SearchGoodsPageState createState() => _SearchGoodsPageState();
}

class _SearchGoodsPageState extends State<SearchGoodsPage> {
  TextEditingController _editingController = TextEditingController();
  List<GoodsModel> _goods;
  GoodsService _goodsService = GoodsService();

  @override
  Widget build(BuildContext context) {
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light.copyWith(
      statusBarColor:
          AppColors.mainColor, //or set color with: Color(0xFF0000FF)
    ));

    Widget _titleSearch() {
      return ConstrainedBox(
          constraints: BoxConstraints(maxHeight: 35),
          child: Row(
            children: <Widget>[
              Container(
                padding: EdgeInsets.only(right: 3),
              ),
              Expanded(
                  child: TextField(
                      autofocus: false,
                      onEditingComplete: _searchKeyWords,
                      controller: _editingController,
                      textInputAction: TextInputAction.search,
                      scrollPadding: EdgeInsets.only(left: 0),
                      style: TextStyle(fontSize: 12),
                      decoration: InputDecoration(
                        contentPadding:
                            const EdgeInsets.symmetric(vertical: 2.0),
                        hintText: '请输入搜索内容',
                        hintStyle: TextStyle(fontSize: 12),
                        prefixIcon: Icon(
                          Icons.search,
                          color: AppColors.mainColor,
                        ),
                        border: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(32),
                            borderSide: BorderSide.none),
                        filled: true,
                        fillColor: Colors.white,
                      )))
            ],
          ));
    }

    return Scaffold(
      appBar: PreferredSize(
          child: AppBar(
            automaticallyImplyLeading: false,
            titleSpacing: 8,
            title: _titleSearch(),
            actions: <Widget>[
              GestureDetector(
                onTap: () {
                  Navigator.pop(context);
                },
                child: Container(
                  padding: EdgeInsets.only(right: 8),
                  child: Center(
                    child: Text(
                      "取消",
                      style: TextStyle(fontSize: 12, color: Colors.white),
                    ),
                  ),
                ),
              )
            ],
          ),
          preferredSize: Size.fromHeight(ScreenUtil.instance.height * 0.036)),
      body: _goods == null || _goods.length == 0
          ? EmptyView(
              topPadding: 30,
            )
          : GridView.builder(
              itemCount: _goods.length,
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  childAspectRatio: 0.7,
                  crossAxisSpacing: ScreenUtil.instance.setWidth(10.0),
                  mainAxisSpacing: ScreenUtil.instance.setHeight(10.0),
                  crossAxisCount: 2),
              itemBuilder: (BuildContext context, int index) {
                return getGoodsItemView(_goods[index]);
              }),
    );
  }

  _searchKeyWords() {
    _goodsService.searchGoods(_editingController.text.toString(), 1, (success) {
      setState(() {
        _goods = success;
      });
    }, onFail: (error) {
      setState(() {
        _goods = null;
      });
    });
  }

  _goGoodsDetail(int goodsId) {
    NavigatorUtils.goGoodsDetailPage(context, goodsId);
  }

  Widget getGoodsItemView(GoodsModel goodsEntity) {
    return InkWell(
      onTap: () => _goGoodsDetail(goodsEntity.id),
      child: Card(
          child: Column(
            children: <Widget>[
              Expanded(
                  child: CachedImageView(double.infinity,
                      ScreenUtil.instance.setHeight(360.0), goodsEntity.url)),
              Padding(
                padding: EdgeInsets.only(
                    top: ScreenUtil.getInstance().setHeight(10.0)),
              ),
              Padding(
                padding: const EdgeInsets.only(left: 5,right: 5),
                child: Text(
                  goodsEntity.name,
                  maxLines: 3,
                  style: TextStyle(
                      fontSize: ScreenUtil.getInstance().setSp(26.0),
                      color: Colors.black54),
                ),
              ),
            ],
          ))
    );
  }

  Widget _keyWordView(String keyword) {
    return Container(
      width: double.infinity,
      alignment: Alignment.centerLeft,
      padding: EdgeInsets.only(left: ScreenUtil.instance.setWidth(20.0)),
      height: ScreenUtil.instance.setHeight(80.0),
      child: InkWell(
        child: Text(
          keyword,
          style: TextStyle(
              color: Colors.black54, fontSize: ScreenUtil.instance.setSp(26.0)),
        ),
      ),
    );
  }
}
